package com.example.musicplayer.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.example.musicplayer.view.SlideLinearLayout;

public abstract class SlideListViewAdapter extends BaseAdapter {
    protected Context mContext;
    protected ListView mListView;
    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View parent, View view, int position, long id);
    }

    public SlideListViewAdapter(Context context, ListView listView) {
        mContext = context;
        mListView = listView;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    //resume the beginning state
    public void resume() {
        if (mListView != null) {
            for (int i = 0; i < mListView.getChildCount(); i++) {
                View child = mListView.getChildAt(i);
                if (child instanceof SlideLinearLayout) {
                    ((SlideLinearLayout) child).scrollTo(0, 0);
                }
            }
        }
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        resume();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = bindView(position, convertView, parent);
        //side view listener
        if (convertView instanceof SlideLinearLayout) {
            //resume
            ((SlideLinearLayout) convertView).resume();
            ((SlideLinearLayout) convertView).setStateListener(new SlideLinearLayout.StateListener() {
                @Override
                public boolean ignoreDownEvent() {
                    View child = isSlide(position);
                    if (child instanceof SlideLinearLayout) {
                        ((SlideLinearLayout) child).resume();
                    }
                    return child instanceof SlideLinearLayout;
                }
            });
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(mListView, v, position, getItemId(position));
                    }
                }
            });
        }
        return convertView;
    }

    protected View isSlide(int currentPosition) {
        if (mListView == null) {
            return null;
        }
        currentPosition = currentPosition - mListView.getFirstVisiblePosition();
        for (int i = 0; i < mListView.getChildCount(); i++) {
            if (i != currentPosition) {
                View child = mListView.getChildAt(i);
                if (child instanceof SlideLinearLayout && ((SlideLinearLayout) child).isSlide()) {
                    return child;
                }
            }
        }
        return null;
    }

    protected abstract View bindView(int position, View convertView, ViewGroup parent);
}